package com.media.themovieworld.screen.detail;

import java.util.ArrayList;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.media.themovieworld.R;
import com.media.themovieworld.api.ApiParams;
import com.media.themovieworld.common.fragment.MovieTaskScreen;
import com.media.themovieworld.model.MovieItemModel;
import com.media.themovieworld.model.PageItemInfo;
import com.media.themovieworld.player.FilmPartManage;
import com.media.themovieworld.player.MoviePlayerActivity;
import com.media.themovieworld.util.CommonUtil;
import com.media.themovieworld.util.MovieImageLoader;
import com.media.themovieworld.widget.viewpagerindicator.TitlePageIndicator;

public class MovieDetailScreen extends MovieTaskScreen{

	private MovieItemModel mMovieData;
	/*VIEW*/
	private TextView tName;
	private ImageView imgPoster;
	private ImageView imgNew;
	private ImageButton btnPlayMovie;
	private TextView tvTypeName;
	private TextView tvDuration;
	
	private ProgressBar mProgressBar;
	
	/** The m pager. */
	private ViewPager mPager;
	
	private FragmentPagerAdapter mAdapter;
	
	private ArrayList<PageItemInfo> mListPageInfo;
	
	/** The m indicator. */
	private TitlePageIndicator mIndicator;
	
//	public void setData(MovieItemModel data){
//		mMovieData = data;
//		FilmPartManage.getInstance().setCurrentFilmData(mMovieData);
//		if (mMovieData==null ||mMovieData.filmPartList == null) {
//			FilmPartManage.getInstance().setCurrentFilmPart(null);
//		}else{
//			FilmPartManage.getInstance().setCurrentFilmPart(mMovieData.filmPartList);
//		}
//	}
	
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View v = inflater.inflate(R.layout.detail_film_screen, null);
		mMovieData = FilmPartManage.getInstance().getCurrentFilmData();
		tName = (TextView) v.findViewById(R.id.tmw__dfa_tName);
		imgPoster = (ImageView) v.findViewById(R.id.tmw__dfa_iPoster);
		imgNew	  = (ImageView) v.findViewById(R.id.tmw__dfa_iNew);
		mProgressBar = (ProgressBar) v.findViewById(R.id.tmw__dfa_progressBar);
		mIndicator = (TitlePageIndicator) v.findViewById(R.id.tmw__dfa_detail_indicator);
		mPager	   = (ViewPager) v.findViewById(R.id.tmw__dfa_detail_pager);
		btnPlayMovie = (ImageButton) v.findViewById(R.id.tmw__dfa_bXemphim);
		tvTypeName = (TextView) v.findViewById(R.id.tmw__dfa_tTheloai);
		tvDuration = (TextView) v.findViewById(R.id.tmw__dfa_tDuration);
		btnPlayMovie.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), MoviePlayerActivity.class);
				intent.putExtra("index", 0);
				getActivity().startActivity(intent);
			}
		});
		initDatas();
		return v;
	}
	
	private void initDatas(){
		if(mMovieData==null) return;
		tName.setText(mMovieData.name);
		tvTypeName.setText(getResources().getString(R.string.category)+" : "+mMovieData.typeName);
		tvDuration.setText(CommonUtil.convertToTimeString(mMovieData.showTime));
		loadPoster(mMovieData.icon);
		if (mMovieData.isNew == 0) {
			imgNew.setVisibility(View.INVISIBLE);
		}else{
			imgNew.setVisibility(View.VISIBLE);
		}
		loadListPageInfo();
		mAdapter =  new ViewPagerDetailAdapter(getFragmentManager());
		mPager.setAdapter(mAdapter);
		mIndicator.setViewPager(mPager);
		mPager.setCurrentItem(1);
		mProgressBar.setVisibility(View.INVISIBLE);
	}
	
	private void loadPoster(String id){
		int imageWidth = getResources().getDimensionPixelSize(R.dimen.movie_detail_thumnail_width);
		int imageHeight = getResources().getDimensionPixelSize(R.dimen.movie_detail_thumnail_height);
		String url = ApiParams.getDownloadImageLink(imageWidth, imageHeight,id);
		MovieImageLoader.getInstance().displayImage(url, imgPoster, MovieImageLoader.getDisplayImageOptionDefault());
	}
	
	/**
	 * Load list page info.
	 */
	private void loadListPageInfo(){
		mListPageInfo = new ArrayList<PageItemInfo>();
		PageItemInfo watchMoviePagerInfo = new PageItemInfo();
		watchMoviePagerInfo.pageTitle = getResources().getString(R.string.play);
		watchMoviePagerInfo.fragClass = WatchMoviePagerScreen.class;
		
		PageItemInfo moviePagerInfo = new PageItemInfo();
		moviePagerInfo.pageTitle = getResources().getString(R.string.movie_info);
		moviePagerInfo.fragClass = MoviePagerInfoScreen.class;
		
		PageItemInfo movieSuggestPagerInfo = new PageItemInfo();
		movieSuggestPagerInfo.pageTitle = getResources().getString(R.string.suggest_movies);
		movieSuggestPagerInfo.fragClass = MovieSuggestPagerScreen.class;
		
		mListPageInfo.add(watchMoviePagerInfo);
		mListPageInfo.add(moviePagerInfo);
		mListPageInfo.add(movieSuggestPagerInfo);
	}
	
	public class ViewPagerDetailAdapter extends FragmentPagerAdapter{
		
		
		public ViewPagerDetailAdapter(FragmentManager fm) {
	        super(fm);
	        if (mListPageInfo == null) {
				mListPageInfo = new ArrayList<PageItemInfo>();
			}
	    }

	    /* (non-Javadoc)
		 * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
		 */
		@Override
	    public Fragment getItem(int position) {
	    	Fragment frag = null;
			try {
				frag = (Fragment) mListPageInfo.get(position).fragClass.newInstance();
			} catch (java.lang.InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
	        return frag;
	    }

	    /* (non-Javadoc)
		 * @see android.support.v4.view.PagerAdapter#getCount()
		 */
		@Override
	    public int getCount() {
	        return mListPageInfo.size();
	    }

	    /* (non-Javadoc)
		 * @see android.support.v4.view.PagerAdapter#getPageTitle(int)
		 */
		@Override
	    public CharSequence getPageTitle(int position) {
	      return mListPageInfo.get(position).pageTitle;
	    }
	}
	
}
